﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Text;

using FRL.DebtChased.Extensions;

namespace FRL.DebtChased.Repository.ViewModels
{

	/// <summary>
	/// Declaration of AccountTradingStyleModel class
	/// </summary>
	[Serializable]
	public class AccountTradingStyleModel
	{

		/////////////////////////////////////////////////////////////
		// Nested classes

		[Serializable]
		public class Element
		{

			/////////////////////////////////////////////////////////
			// Attributes

			internal int m_iAccountTradingStyleId = 0;
			public int AccountTradingStyleId
			{
				get { return m_iAccountTradingStyleId; }
			}

			internal string m_strName;
			public string Name
			{
				get { return m_strName; }
				set { m_strName = value; }
			}

			internal bool m_bInUse = false;
			public bool InUse
			{
				get { return m_bInUse; }
			}
			public string InUseAsText
			{
				get { return m_bInUse?"Yes":"No"; }
			}

			internal string m_strModifiedAsText;
			public string ModifiedAsText
			{
				get { return m_strModifiedAsText; }
			}

			/////////////////////////////////////////////////////////
			// Construction

			public Element(int iAccountTradingStyleModelId,string strName)
			{

				// Initialize member variables
				m_iAccountTradingStyleId = iAccountTradingStyleModelId;
				m_strName = strName;

			}

		}

		/////////////////////////////////////////////////////////////
		// Constants and Enumerations

		/////////////////////////////////////////////////////////////
		// Attributes

		private List<Element> m_elements = new List<Element>();
		public List<Element> Elements
		{
			get { return m_elements; }
		}

		/////////////////////////////////////////////////////////////
		// Construction

		public AccountTradingStyleModel(DebtChasedEntities ent)
		{

			// Load form the repository
			var results = ent.AccountTradingStyleProc();

			// Load the element list
			foreach (var ob in results)
			{

				// Compose last modified date
				string strLastModified = null;
				if(ob.fdModifiedAt != null)
					strLastModified = string.Format("{0} by {1}",ob.fdModifiedAt.Value.ToCuteString(),ob.fdModifiedBy);

				// Load the elements
				Element el = new Element(ob.fdAccountTradingStyleId,ob.fdName)
				{ 
					m_bInUse = (ob.fdUsageCount>0),
					m_strModifiedAsText = strLastModified
				};
				m_elements.Add(el);

			}

		}

		/////////////////////////////////////////////////////////////
		// Operations

		public string Set(DebtChasedEntities ent, int iUserId)
		{

			// Declare return variable
			string strError = null;

			// Get the existing status list
			List<AccountTradingStyle> current = (from ats in ent.AccountTradingStyles select ats).ToList();

			try
			{

				// Update the current items
				foreach (AccountTradingStyle style in current)
				{

					// Find in the model
					AccountTradingStyleModel.Element modelElement = null;
					foreach (AccountTradingStyleModel.Element el in this.m_elements)
					{
						if (el.AccountTradingStyleId == style.AccountTradingStyleId)
						{
							modelElement = el;
							break;
						}
					}

					// If model element found...
					if (modelElement != null)
					{

						// Compare names
						if (modelElement.Name != style.Name)
						{

							// Update the status
							style.Name = modelElement.Name;

						}

					}
					else
					{

						// Status has been deleted
						ent.AccountTradingStyles.DeleteObject(style);

					}

				}

				// Add new status
				foreach (AccountTradingStyleModel.Element el in this.m_elements)
				{

					// If this status has a zero or negative 
					if (el.AccountTradingStyleId <= 0)
					{

						// Add a new status to the database
						AccountTradingStyle newStyle = AccountTradingStyle.CreateAccountTradingStyle(0, el.Name);
						ent.AccountTradingStyles.AddObject(newStyle);
						//session.Add(newStyle, UserAction.eActionType.Insert, newStyle.Name);

					}

				}

				// Save changes to the database
				ent.SaveChanges();

			}
			catch (Exception x)
			{

				// Process exception
				strError = ErrorProcessing.HandleException(x, "save trading style", iUserId);

			}

			// Return variable
			return strError;

		}

		public void SetName(int iAccountTradingStyleModelId, string strName)
		{

			// Find the element
#if DEBUG
			bool bFound = false;
#endif // #if DEBUG
			foreach (Element el in m_elements)
			{
				if (el.AccountTradingStyleId == iAccountTradingStyleModelId)
				{
#if DEBUG
					bFound = true;
#endif // #if DEBUG
					el.Name = strName;
					break;
				}
			}
#if DEBUG
			Debug.Assert(bFound);
#endif // #if DEBUG

		}

	}

}
